/*
 * (C) Copyright 2007
 * Vlad Lungu vlad.lungu@windriver.com
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <common.h>
#include <command.h>
#include <asm/mipsregs.h>
#include <asm/io.h>

#include "asm/yams.h"

/* The structure of the system memory information IO area */
typedef struct {
    uint32_t pages;
} mem_io_area_t;

phys_size_t initdram(int board_type)
{
    io_descriptor_t *io_desc;
	volatile mem_io_area_t *mem_dev_iobase;

    io_desc = (io_descriptor_t *)IO_DESCRIPTOR_AREA;
    
    while(io_desc->type != 0) {
        if (io_desc->type == 0x101) {
            mem_dev_iobase = (mem_io_area_t *) io_desc->io_area_base;
            return (0x1000 * mem_dev_iobase->pages); /* every page is 4k */
        }
        io_desc++;
    }
}

int checkboard(void)
{
	u32 proc_id;
	u32 config1;

	proc_id = read_c0_prid();
	printf("Board: yams mips CPU: ");
	switch (proc_id) {
	case 0x00018000:
		printf("4Kc");
		break;
	case 0x00018400:
		printf("4KEcR1");
		break;
	case 0x00019000:
		printf("4KEc");
		break;
	case 0x00019300:
		config1 = read_c0_config1();
		if (config1 & 1)
			printf("24Kf");
		else
			printf("24Kc");
		break;
	case 0x00019500:
		printf("34Kf");
		break;
	case 0x00000400:
		printf("R4000");
		break;
	case 0x00018100:
		config1 = read_c0_config1();
		if (config1 & 1)
			printf("5Kf");
		else
			printf("5Kc");
		break;
	case 0x000182a0:
		printf("20Kc");
		break;

	default:
		printf("unknown");
	}
	printf(" proc_id=0x%x\n", proc_id);

	return 0;
}

int misc_init_r(void)
{
	set_io_port_base(0);
	return 0;
}

typedef struct {
    uint32_t shutdn;
} shutdn_io_area_t;

/*
 * do_shutdown()
 *
 * Shell command to shutdown the board.
 */
int do_shutdown(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
	printf( "Shutdown...\n" );

    io_descriptor_t *io_desc;
	volatile shutdn_io_area_t *shutdn_dev_iobase;

    io_desc = (io_descriptor_t *)IO_DESCRIPTOR_AREA;
    
    while(io_desc->type != 0) {
        if (io_desc->type == 0x103) {
            shutdn_dev_iobase = (shutdn_io_area_t *) io_desc->io_area_base;
            shutdn_dev_iobase->shutdn = 0x0badf00d;
			/* Should never get here */
			while(1)
				;
        }
        io_desc++;
    }

	return 1;
}

